<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of promotion
 *
 * @author Administrator
 */
class promotion extends CI_Controller {

  //put your code here

  public function __construct() {
    parent::__construct();
    if (!userdata('provider_id')) {
      redirect('provider');
    }
  }

  public function index($max = '', $offset = '') {
    $offset = $offset + 0;
    $max = $this->setting_model->item('max_record');
    $promotions = $this->promotion_model->get_all(array('provider_user_id' => userdata('provider_id')), 
            null, null, null, null, null, null, null, null, null, null, null, array('date_create' => 'desc'), $max, $offset);
    $total = $this->promotion_model->count(array('provider_user_id' => userdata('provider_id')));
    $this->load->helper('paging');
    $paging = paging(site_url('provider/promotion/index'), $total, $max, $offset);
    $this->load->view('provider/promotion/index', array('promotions' => $promotions,
        'offset' => $offset, 'max' => $max, 'paging' => $paging, 'title_html' => $this->lang->line('label.provider.title.promotions')));
  }

  public function create() {
    $promotion_related = $this->promotion_model->get_all(array('provider_user_id' => userdata('provider_id')));
    $this->load->view('provider/promotion/create', array(
        'title_html' => $this->lang->line('label.provider.title.add.promotion'),
        'obj' => array(), 'relate_promotions' => $promotion_related, 'categories' => $this->category_model->get_all_tree()));
  }

  public function save() {
    $object = $_REQUEST;
    $object['start_date'] = empty($object['start_date']) ? '' : strtotime($object['start_date']);
    $object['end_date'] = empty($object['end_date']) ? '' : strtotime($object['end_date']);
    $object['provider_user_id'] = userdata('provider_id');
    $object['date_create'] = $object['date_update'] = time();
    $object['status'] = PROMOTION_NEW;
    $object['remain'] = isset($object['max_download']) ? $object['max_download'] : 0;
    $id = $this->promotion_model->save($object);
    if (is_array($id))
      set_flash_error($id);
    else if (!empty($id)) {
      @mkdir($this->setting_model->item('upload_path') . 'promotion');
      @mkdir($this->setting_model->item('upload_path') . 'promotion/' . $id);
      $base_lib = new Base_lib();
      $images = $base_lib->multiple_upload('images', 'promotion/' . $id);
      if (!empty($images)) {
        foreach ($images as $name => $image) {
          $this->promotion_media_model->save(array('promotion_id' => $id,
              'path' => $image, 'type' => 'image', 'original_name' => $name));
        }
      }
      if (!empty($object['category'])) {
        foreach ($object['category'] as $category) {
          $this->category_promotion_model->save(array('promotion_id' => $id, 'cat_id' => $category, 'date_create' => time()));
        }
      }

      if (!empty($object['promotion2_id'])) {
        foreach ($object['promotion2_id'] as $promotion) {
          $this->promotion_relate_model->save(array('promotion1_id' => $id, 'promotion2_id' => $promotion));
        }
      }
      set_flash_message($this->lang->line('message.created', array($this->lang->line('label.promotion'), $id)));
      redirect('provider/promotion/show/' . $id);
      return;
    }
    $promotion_related = $this->promotion_model->get_all(array('provider_user_id' => userdata('provider_id')));
    $this->load->view('provider/promotion/create', array('obj' => $_REQUEST,
        'title_html' => $this->lang->line('label.provider.title.add.promotion'),
        'relate_promotions' => $promotion_related, 'categories' => $this->category_model->get_all_tree()));
  }

  public function edit($id = '') {
    $promotion = $this->promotion_model->get(array('id' => $id, 'provider_user_id' => userdata('provider_id')));
    if ($promotion) {
      $promotion['start_date'] = empty($promotion['start_date']) ? '' : date($this->setting_model->item('date_format'), $promotion['start_date']);
      $promotion['end_date'] = empty($promotion['end_date']) ? '' : date($this->setting_model->item('date_format'), $promotion['end_date']);
      $category = $this->category_promotion_model->get_all(array('promotion_id' => $id));
      $promotion['category'] = array();
      foreach ($category as $cat)
        $promotion['category'][] = $cat['cat_id'];
      $promotion['promotion2_id'] = array();
      $relate = $this->promotion_relate_model->get_all(array('promotion1_id' => $id));
      foreach ($relate as $r) {
        $promotion['promotion2_id'][] = $r['promotion2_id'];
      }
      $promotion['images'] = $this->promotion_media_model->get_all(array('promotion_id' => $id));
      $promotion_related = $this->promotion_model->get_all(array('provider_user_id' => userdata('provider_id')));
      $this->load->view('provider/promotion/edit', array('obj' => $promotion,
          'title_html' => $this->lang->line('label.provider.title.edit.promotion'),
          'relate_promotions' => $promotion_related, 'categories' => $this->category_model->get_all_tree()));
    } else {
      set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.promotion'), $id)));
      redirect('provider/promotion');
    }
  }

  public function update() {
    if (empty($_REQUEST['id'])) {
      redirect('provider/promotion');
      return;
    }
    $promotion = $this->promotion_model->get(array('id' => $_REQUEST['id'], 'provider_user_id' => userdata('provider_id')));
    if ($promotion) {
      $object = $_REQUEST;
      $object['start_date'] = empty($object['start_date']) ? '' : strtotime($object['start_date']);
      $object['end_date'] = empty($object['end_date']) ? '' : strtotime($object['end_date']);
      $object['date_update'] = time();
      $id = $this->promotion_model->save($object);
      if (is_array($id))
        set_flash_error($id);
      else if (!empty($id)) {
        if (empty($_REQUEST['images_id'])) {
          $this->promotion_media_model->delete(array('promotion_id' => $object['id']));
        } else {
          $this->promotion_media_model->delete(null, null, null, null, array('id' => $_REQUEST['images_id']));
        }
        $base_lib = new Base_lib();
        $images = $base_lib->multiple_upload('images', 'promotion/' . $object['id']);
        if (!empty($images)) {
          foreach ($images as $name => $image) {
            $this->promotion_media_model->save(array('promotion_id' => $object['id'],
                'path' => $image, 'type' => 'image', 'original_name' => $name));
          }
        }
        $this->category_promotion_model->delete(array('promotion_id' => $object['id']));
        if (!empty($object['category'])) {
          foreach ($object['category'] as $category) {
            $this->category_promotion_model->save(array('promotion_id' => $object['id'], 'cat_id' => $category, 'date_create' => time()));
          }
        }
        $this->promotion_relate_model->delete(array('promotion1_id' => $object['id']));
        if (!empty($object['promotion2_id'])) {
          foreach ($object['promotion2_id'] as $promotion) {
            $this->promotion_relate_model->save(array('promotion1_id' => $object['id'], 'promotion2_id' => $promotion));
          }
        }
        set_flash_message($this->lang->line('message.updated', array($this->lang->line('label.promotion'), $object['id'])));
        redirect('provider/promotion/show/' . $object['id']);
        return;
      }
      $_REQUEST['images'] = $this->promotion_media_model->get_all(array('promotion_id' => $_REQUEST['id']));
      $promotion_related = $this->promotion_model->get_all(array('provider_user_id' => userdata('provider_id')));
      $this->load->view('provider/promotion/edit', array('obj' => $_REQUEST,
          'title_html' => $this->lang->line('label.provider.title.edit.promotion'),
          'relate_promotions' => $promotion_related, 'categories' => $this->category_model->get_all_tree()));
    } else {
      set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.promotion'), $_REQUEST['id'])));
      redirect('provider/promotion');
    }
  }

  public function show($id = '') {
    $promotion = $this->promotion_model->get(array('id' => $id, 'provider_user_id' => userdata('provider_id')));
    if ($promotion) {
      $category = $this->category_promotion_model->get_all(array('promotion_id' => $id));
      $promotion['category'] = array();
      foreach ($category as $cat)
        $promotion['category'][] = $cat['cat_id'];
      $promotion['promotion2_id'] = array();
      $relate = $this->promotion_relate_model->get_all(array('promotion1_id' => $id));
      foreach ($relate as $r) {
        $promotion['promotion2_id'][] = $r['promotion2_id'];
      }
      $promotion['images'] = $this->promotion_media_model->get_all(array('promotion_id' => $id));
      $this->load->view('provider/promotion/show', array('obj' => $promotion,
          'title_html' => $this->lang->line('label.provider.title.detail.promotion')));
      return;
    } else {
      set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.promotion'), $id)));
      redirect('provider/promotion');
    }
  }

  public function delete($id = '') {
    $promotion = $this->promotion_model->get(array('id' => $id, 'provider_user_id' => userdata('provider_id')));
    if ($promotion) {
      @deletedir($this->setting_model->item('upload_path') . 'promotion/' . $id);
      $this->promotion_media_model->delete(array('promotion_id' => $id));
      $this->promotion_download_model->delete(array('promotion_id' => $id));
      $this->promotion_view_model->delete(array('promotion_id' => $id));
      $this->category_promotion_model->delete(array('promotion_id' => $id));
      $this->discussion_model->delete(array('promotion_id' => $id));
      $this->promotion_favorite_model->delete(array('promotion_id' => $id));
      $this->promotion_relate_model->delete(null, array('promotion1_id' => $id), array('promotion2_id' => $id));
      $this->promotion_model->delete($promotion);
      set_flash_message($this->lang->line('message.deleted', array($this->lang->line('label.promotion'), $id)));
    } else {
      set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.promotion'), $id)));
    }
    redirect('provider/promotion');
  }

  public function search($keyword = '', $max = '', $offset = '') {
    $keyword = empty($keyword) ? (isset($_GET['keyword']) ? $_GET['keyword'] : '') : $keyword;
    $offset = $offset + 0;
    $max = $this->setting_model->item('max_record');
    $promotions = $this->promotion_model->get_all(array('provider_user_id' => userdata('provider_id')), null, null, null, null, null, null, array('title' => $keyword, 'description' => $keyword, 'code' => $keyword), null, null, null, null, array('date_create' => 'desc'), $max, $offset);
    $total = $this->promotion_model->count(array('provider_user_id' => userdata('provider_id')), null, null, null, null, null, null, array('title' => $keyword, 'description' => $keyword, 'code' => $keyword));
    $this->load->helper('paging');
    $paging = paging(site_url('provider/promotion/search'), $total, $max, $offset, $keyword);
    $this->load->view('provider/promotion/index', array('promotions' => $promotions,
        'offset' => $offset, 'max' => $max, 'paging' => $paging, 'keyword' => $keyword));
  }

  public function check($download_code = '') {
    if(!empty($_POST['download_code'])){
      redirect('provider/promotion/check/'.$_POST['download_code']);
      return;
    }
    if (!empty($download_code)) {
      $promotion_download = $this->promotion_download_model->get(array('code' => $download_code));
      if (!empty($promotion_download)) {
        $promotion = $this->promotion_model->get(array('id' => $promotion_download['promotion_id'],
            'provider_user_id' => userdata('provider_id')));
        if ($promotion) {
          $category = $this->category_promotion_model->get_all(array('promotion_id' => $promotion_download['promotion_id']));
          $promotion['category'] = array();
          foreach ($category as $cat)
            $promotion['category'][] = $cat['cat_id'];
          $promotion['promotion2_id'] = array();
          $relate = $this->promotion_relate_model->get_all(array('promotion1_id' => $promotion_download['promotion_id']));
          foreach ($relate as $r) {
            $promotion['promotion2_id'][] = $r['promotion2_id'];
          }
          $promotion['images'] = $this->promotion_media_model->get_all(array('promotion_id' => $promotion_download['promotion_id']));
          $this->load->view('provider/promotion/check', array('promotion' => $promotion,
              'download_code' => $download_code, 'download' => $promotion_download,
              'title_html' => $this->lang->line('label.provider.title.detail.promotion')));
          return;
        } else {
          set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.promotion'), $promotion_download['promotion_id'])));
          $this->load->view('provider/promotion/check', array(
              'download_code' => $download_code,
              'title_html' => $this->lang->line('label.provider.title.detail.promotion')
          ));
        }
      } else {
        set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.promotion.download.code'), $download_code)));
        $this->load->view('provider/promotion/check', array(
            'download_code' => $download_code,
            'title_html' => $this->lang->line('label.provider.title.detail.promotion')
        ));
      }
    } else {
      $this->load->view('provider/promotion/check', array('title_html' => $this->lang->line('label.provider.title.detail.promotion')));
    }
  }

  public function get_promotion($download_code = '') {
    if (empty($download_code) && isset($_POST['download_code'])) {
      $download_code = $_POST['download_code'];
    }
    if (!empty($download_code)) {
      $promotion_download = $this->promotion_download_model->get(array('code' => $download_code));
      if (!empty($promotion_download)) {
        $promotion_download['status'] = PROMOTION_DOWNLOAD_GOT;
        $this->promotion_download_model->save($promotion_download);
        redirect('provider/promotion/check/'.$download_code);
      } else {
        set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.promotion.download.code'), $download_code)));
        $this->load->view('provider/promotion/check', array(
            'download_code' => $download_code,
            'title_html' => $this->lang->line('label.provider.title.detail.promotion')
        ));
      }
    } else {
      redirect('provider/promotion/check');
    }
  }

}